package cybertechcebu.task
{
	import cybertechcebu.Console;
	import cybertechcebu.FTPClient;
	import cybertechcebu.FTPCommand;
	import cybertechcebu.FTPResponse;
	import cybertechcebu.LocalFile;
	import cybertechcebu.enum.Commands;
	import cybertechcebu.enum.FileStatus;
	import cybertechcebu.enum.Responses;
	
	import mx.controls.Alert;
	import mx.core.mx_internal;
	import mx.utils.object_proxy;
	
	public class SizeTask extends Task
	{
		private var fileToCheck:LocalFile;
		
		public function SizeTask(client:FTPClient, fileToCheck:LocalFile)
		{
			super(client);
			this.fileToCheck = fileToCheck;
		}
		
		override protected function startTask():void
		{
			sendCommand(new FTPCommand(Commands.SIZE, fileToCheck.name));
		}
		
		override public function handleResponse(response:FTPResponse):void
		{
			switch (response.code)
			{
				case Responses.FILE_STATUS:
					
					var size:String = response.str.substring(4, response.str.length);
					if (parseInt(size) == fileToCheck.size)
					{
						fileToCheck.status = FileStatus.SUCCESS;
						dispatchTaskEnd(true);
					}
					else
					{
						fileToCheck.status = FileStatus.FAIL;
						dispatchTaskEnd(false);
					}					
					break;
				case Responses.DATA_CONN_CLOSE:
					break;
				default:
					dispatchTaskError("Unhandled response code: " + response.code);
			}
		}
	}
}
